import 'package:flutter/material.dart';
import 'package:heven/pages/home/widget/cd/index.dart';
import 'package:heven/pages/home/widget/music/index.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key, this.arguments});

  final arguments;

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _currentIndex = 0;

  // pritf widget.arguments['index']

  late PageController _pageController;

  List<Widget> pages = [
    MusicWidget(),
    CdWidget(),
  ];
  @override
  void initState() {
    super.initState();
    try {
      if (widget.arguments['index'] != null) {
        setState(() {
          _currentIndex = widget.arguments['index'];
        });
      }
    } catch (e) {}

    _pageController = PageController(initialPage: _currentIndex);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView.builder(
        physics: const NeverScrollableScrollPhysics(),
        controller: _pageController,
        itemBuilder: (BuildContext context, int index) {
          return Container(
            child: pages[index],
          );
        },
        onPageChanged: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
      bottomNavigationBar: Container(
        decoration: const BoxDecoration(
          color: Color(0xFF2C2B30),
          borderRadius: BorderRadius.only(
              topRight: Radius.circular(15), topLeft: Radius.circular(15)),
        ),
        child: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          selectedItemColor: const Color(0xFFFFFFFF),
          unselectedItemColor: const Color(0xFFCDCDCD),
          currentIndex: _currentIndex,
          selectedFontSize: 11.0,
          unselectedFontSize: 11.0,
          iconSize: 28,
          backgroundColor: Colors.transparent,
          onTap: (index) {
            _currentIndex = index;
            _pageController.animateToPage(index,
                duration: const Duration(milliseconds: 1),
                curve: Curves.bounceIn);

            setState(() {});
          },
          items: [
            BottomNavigationBarItem(
              icon: Padding(
                padding: EdgeInsets.only(bottom: 3),
                child: Image.asset(
                  _currentIndex == 0
                      ? 'assets/images/music_active.png'
                      : 'assets/images/music.png',
                  width: 20,
                  height: 20,
                ),
              ),
              label: '探索',
            ),
            BottomNavigationBarItem(
              icon: Padding(
                padding: EdgeInsets.only(bottom: 3),
                child: Image.asset(
                  _currentIndex == 1
                      ? 'assets/images/cd_active.png'
                      : 'assets/images/cd.png',
                  width: 20,
                  height: 20,
                ),
              ),
              label: '作品',
            ),
          ],
        ),
      ),
      floatingActionButton: SizedBox(
        height: 60,
        width: 60,
        child: FloatingActionButton(
          backgroundColor: Colors.transparent,
          elevation: 0,
          shape: const CircleBorder(),
          onPressed: () {
            Navigator.pushNamed(context, '/creation');
          },
          child: Container(
            child: Image.asset(
              'assets/images/add.png',
            ),
          ),
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }
}
